<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span
        class="comment"
        @click="$router.push({ path: `/comment/${article.id}` })"
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ article.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        :class="{ active: article.has_star }"
        @click="postStar"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5" v-model="comment"></textarea>
      <div>
        <span @click="send">发 送</span>
        <span @click="reset">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { sendComment } from "@/api/post";
export default {
  props: {
    article: {
      type: Object,
      require: true,
    },
    commentObj: {
      type: Object,
    },
  },
  watch: {
    commentObj() {
      //判断commentObj是否有值，如果为空 不需要弹出
      if (this.commentObj) {
        this.isFocus = true;
      }
    },
  },
  data() {
    return {
      isFocus: false,
      comment: "",
    };
  },
  methods: {
    reset() {
      this.isFocus = false;
      //发出事件，让父组件清空commentObj对象，不然 点击回复 在取消 在回复 不会出现弹框
      this.$emit("reset");
    },

    handlerFocus() {
      this.isFocus = true;
    },
    postStar(e) {
      // this.article.has_star = true;
      this.$emit("click", e);
    },
    //发表评论
    async send(e) {
      //判断评论内容是否为空
      if (this.comment.trim().length == 0) {
        this.$toast.fail("请输入内容");
        return;
      }
      let data = { content: this.comment };
      //判断是否为回复内容
      if (this.commentObj) {
        data.parent_id = this.commentObj.id;
      }
      let res = await sendComment(this.article.id, data);
      //收起输入框
      this.isFocus = false;
      //清空输入框
      this.comment = "";
      //发出事件，让父组件刷新
      this.$emit("refresh", e);
      //console.log(res);
      //提示
      this.$toast.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
}
.active {
  color: rgb(243, 165, 19);
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  background-color: #fff;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>